import http from "../../utils/http"

// pages/emo6/emo6.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        option1: [],
        page: 1,
        totalPages: 1,
        loading: false,
        hasMore: true,
        activeNames: ['1'],

    },
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    fn(e) {
        console.log(e.currentTarget.dataset.id);
        let id=e.currentTarget.dataset.id
        http({
            url: `/student/course/select/` + id,
            method: 'post'
        }).then(res => {
            console.log(res);
            if (res.code === 0) {
                this.setData({
                 title:'报名成功',
                 message:'success'
                })
                this.getList()
            }
        })
    },
    /**
     * 获取课程列表
     */
    getList() {
        // 如果正在加载，直接返回
        if (this.data.loading) return;
        // 设置加载状态
        this.setData({
            loading: true
        });

        http({
            url: `/student/course/select/page/${this.data.page}`
        }).then(res => {
            console.log(res.data);

            if (res.code === 0) {
                // 根据实际数据结构调整赋值逻辑
                const {
                    records,
                    totalPages
                } = res.data;

                this.setData({
                    option1: records || [], // 确保 option1 为数组
                    totalPages: totalPages || 1,
                    hasMore: this.data.page < (totalPages || 1),
                    loading: false,
                    option1: res.data
                });
            } else {
                wx.showToast({
                    title: res.msg || '获取数据失败',
                    icon: 'none'
                });
                this.setData({
                    loading: false
                });
            }
        }).catch(err => {
            console.error('网络请求错误:', err);
            wx.showToast({
                title: '网络请求错误',
                icon: 'none'
            });
            this.setData({
                loading: false
            });
        });
    },

    /**
     * 下拉刷新处理
     */
    onPullDownRefresh() {
        this.setData({
            page: 1
        }, () => {
            this.getList();
            // 刷新完成后停止下拉刷新动画
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 上拉加载更多处理
     */
    onReachBottom() {
        if (!this.data.hasMore || this.data.loading) return;

        this.setData({
            page: this.data.page + 1
        }, () => {
            this.getList();
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList();
    }
})